package com.m2soft.controls.flashviewer
{
	import com.hexagonstar.util.debug.Debug;
	import com.m2soft.streaming.ReportLoader;
	
	import flash.accessibility.AccessibilityProperties;
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;
	import flash.utils.*;
	
	import kr.co.m2soft.model.CommonModel;
	import kr.co.m2soft.model.document.MML;
	
	import mx.controls.Alert;
	import mx.resources.*;
	import mx.utils.ObjectUtil;

	// RD Report object
	public class RDReport implements IEventDispatcher
	{
		
		private var dispatcher:IEventDispatcher = new EventDispatcher();

		// report contents
		private var m_arrLoader : Array = new Array();		// array of Loader for (page count) = page contents
															//		valid: Loader, invalid : null
		
		// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
		private var m_mmlHead: String;
		private var m_arrMmlPage: Array = new Array();		// array of page = page mml contents
		
		private var m_curPage : int = 0;
		private var m_reportByteArray : ByteArray = null;
		private var m_reportType : String = null;
				
		// load status
		private var m_reportLoader : ReportLoader = new ReportLoader();

		static public const STATUS_INIT 		: int = 0;
		static public const STATUS_CREATING 	: int = 1;
		static public const STATUS_CHECKING 	: int = 2;
		static public const STATUS_COMPLETED 	: int = 3;
		static public const STATUS_CANCELED 	: int = 4;

		private var m_load_status : int = STATUS_INIT;
		private var m_save_report : Boolean = false;
		
		// extra parameter
		private var m_url : String;
		private var m_mrd_path : String;
		private var m_mrd_param : String;
		private var m_mrd_data : String;
		private var m_rdid : String;
		private var m_pageCountChangedHandler : Function;
		private var m_statusChangedHandler : Function;
		private var m_pageChangedHandler : Function;
		private var m_documentLoadedErrorHandler : Function;	
		private var m_httpResponseHandler : Function;
		private var m_viewshowmode : int;
		
		// kth add 2012.04.12
		private var arryAccessibilityTxt : Array = new Array();
		public var accessibility_active: Boolean = false;
		private var updateAccessibilityPropHandler: Function = null;
		private var accessibility_opt: int = 0;
		
		// kth #1461 [한국대학교육협의회] Flash Reporting 동작 방식 변경
		private var swfStream_opt: int = 0;
		
		// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
		private var mmlType: int = 0;
		
		private var commonModel:CommonModel = CommonModel.getInstance();
		// 
		public function RDReport()
		{
			super();
		}
		
		//
		public function Clear() : void
		{
			m_reportLoader.Clear();
			var s:XML = <MML version="1.0"><HEAD></HEAD><BODY></BODY></MML>;
			commonModel.mml = new MML();
			commonModel.mml.dataProvider = s;
			commonModel.mml.parser();
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			if (mmlType == 1) {
				m_mmlHead = "";
				m_arrMmlPage = new Array();
				
			}
			else
				m_arrLoader = new Array();
			
			m_curPage = 0;
			changeStatus(STATUS_INIT, false);
		}
		
		//
		public function setParam(
				url : String, 
				mrd_path : String,
				mrd_param : String,
				mrd_data : String,
				loaderCtx : LoaderContext,
				pageCountChangedHandler : Function,
				statusChangedHandler : Function,
				pageChangedHandler : Function,
				documentLoadedErrorHandler : Function,
				responseHandler : Function,
				viewshowmode : int 
			) : void
		{
			m_url = url;
			m_mrd_path = mrd_path;
			m_mrd_param = mrd_param;
			m_mrd_data = mrd_data;
			m_pageCountChangedHandler = pageCountChangedHandler;
			m_statusChangedHandler = statusChangedHandler;
			m_pageChangedHandler = pageChangedHandler; // kth #1461 [한국대학교육협의회] Flash Reporting 동작 방식 변경
			m_documentLoadedErrorHandler = documentLoadedErrorHandler;
			m_httpResponseHandler = responseHandler;
			m_viewshowmode = viewshowmode;

			// kth #1461 [한국대학교육협의회] Flash Reporting 동작 방식 변경
			//m_reportLoader.setParam(loaderCtx, httpResponseHandler, pageLoadHandler, documentLoadedErrorHandler, httpProgressHandler);
			
			// kth #1461 [한국대학교육협의회] Flash Reporting 동작 방식 변경
			m_reportLoader.setParam(loaderCtx, httpResponseHandler, pageLoadHandler, documentLoadedErrorHandler, httpProgressHandler, httpMmlHandler);
		}
		
		// load Report
		public function loadReport() : void
		{
			m_curPage = 0;
			
			changeStatus(STATUS_CREATING, false);
			
			var req : URLRequest = new URLRequest(); 
			req.url = m_url; 
			
			var params : URLVariables = new URLVariables();
			
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			if (mmlType == 0)
			{
				// kth add 2012.04.12
				if (accessibility_opt == 1)
				{
					m_mrd_param += " /rswfsavetext [2]";
					params["mrd_param"] = m_mrd_param;
					setAccessibilityActive(true);
				}
				else if (accessibility_opt == 2)
				{
					setAccessibilityActive(false);
				} 
				else 
				{
					if (flash.accessibility.Accessibility.active)
					{
						m_mrd_param += " /rswfsavetext [2]";
						params["mrd_param"] = m_mrd_param;
						setAccessibilityActive(true);
					}
					else
					{
						setAccessibilityActive(false);
					}
				}
				
				params["User-Agent"] = "RD Flash Viewer";
				if (swfStream_opt == 1) {
					params["opcode"] = "607";
					params["swf_mode"] = "1";
				}
				else if (swfStream_opt == 2) {
					params["opcode"] = "607";
					params["swf_mode"] = "2";
				}
				else {
					params["opcode"] = "600";
				}
				
				params["export_type"] = "swf";
				params["mrd_path"] = m_mrd_path;
				params["mrd_param"] = m_mrd_param;
				params["mrd_data"] = m_mrd_data;
				params["viewshowmode"] = m_viewshowmode;
				
				req.data = params; 
				req.method=URLRequestMethod.POST;
				
				if (swfStream_opt != 0)
					m_reportLoader.loadStream(req);
				else
					m_reportLoader.load(req);
			}
			else 
			{
				params["User-Agent"] = "RD Flash Viewer";
				params["opcode"] = "700";
				params["mrd_path"] = m_mrd_path;
				params["mrd_param"] = m_mrd_param;
				
				req.data = params; 
				req.method=URLRequestMethod.POST;
				
				m_reportLoader.loadMml(req);
			}
		}
		
		//
		public function SaveReportFile(type : String, ownerPassword : String, userPassword : String) : void
		{
			if (m_load_status != STATUS_CANCELED && m_load_status != STATUS_CHECKING && m_load_status != STATUS_COMPLETED)
				return;
				
			changeStatus(m_load_status, true);
			
			var req : URLRequest = new URLRequest(); 
			req.url = m_url; 

			var params : URLVariables = new URLVariables();
			params["User-Agent"] = "RD Flash Viewer";
			params["export_type"] = type;
			params["opcode"] = "603";
			if (swfStream_opt != 0) {
				params["mrd_path"] = m_mrd_path;
				params["mrd_param"] = m_mrd_param;
		    }
			else {
				params["rdid"] = m_rdid;
			}
            
            if (type == "pdf" && ownerPassword != null)
            	params["ownerpassword"] = ownerPassword;
            
            if (type == "pdf" && userPassword != null)
            	params["userpassword"] = userPassword;
            
			req.data = params; 
			req.method=URLRequestMethod.POST;
			 
			m_reportLoader.load(req);
		}

		// 
		public function getReportFile(type : String, ownerPassword : String, userPassword : String) : String
		{
			var url : String;
			
			if (swfStream_opt != 0)
				url = m_url + "?User-Agent=RD Flash Viewer&opcode=605&export_type=" + type + "&mrd_path=" + m_mrd_path + "&mrd_param=" + m_mrd_param ;
			else
				url = m_url + "?User-Agent=RD Flash Viewer&opcode=605&export_type=" + type + "&rdid=" + m_rdid;
            
            if (type == "pdf" && ownerPassword != null)
            	url += "&ownerpassword=" + ownerPassword;
            
            if (type == "pdf" && userPassword != null)
            	url += "&userpassword=" + userPassword;

			return url;            
		}	
			
		//
		public function cancelLoad() : void
		{
			m_reportLoader.clearReportRequest();
			
			if (swfStream_opt != 0) {
				if (m_load_status == STATUS_CHECKING || m_load_status == STATUS_CREATING)
					m_reportLoader.progressInit(STATUS_CANCELED);
			}
			else {
				var req : URLRequest = new URLRequest(); 
				req.url = m_url; 
	
				var params : URLVariables = new URLVariables();
				params["User-Agent"] = "RD Flash Viewer";
				params["opcode"] = "604";
				params["rdid"] = m_rdid;
	            
				req.data = params; 
				req.method=URLRequestMethod.POST;
				 
				m_reportLoader.load(req);
	
				changeStatus(m_load_status, false);
			}
		}
		
		//  startpage : 1, 2,...	endpage : 0=all, 1, 2, ...
		public function isPageLoaded(startpage : int, endpage : int) : Boolean
		{
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			var loadArray: Array;
			
			if (mmlType == 1)
				loadArray = m_arrMmlPage;
			else
				loadArray = m_arrLoader;
			
			startpage = Math.max(0, startpage-1);
			endpage = Math.min(endpage-1, loadArray.length - 1);
			
			if (endpage == -1)
				endpage = loadArray.length - 1;
				
			for(var i : int=startpage; i<=endpage; i++)
			{
				if (loadArray[i] == null)
					return false;
			}
			
			return true;				
		}
		
		// check completed		
		private function checkCompleted() : void
		{
			if (m_load_status != STATUS_CHECKING)
				return;
			
			if (swfStream_opt != 0)
				return;
				
			var req : URLRequest = new URLRequest(); 
			req.url = m_url; 

			var params : URLVariables = new URLVariables();
			params["User-Agent"] = "RD Flash Viewer";
			params["opcode"] = "601";
			params["rdid"] = m_rdid;
            
			req.data = params; 
			req.method=URLRequestMethod.POST;
			 
			m_reportLoader.load(req);

			flash.utils.setTimeout(checkCompleted, 1000);
		}
		
		// load Page (pagenum : 1, 2, 3 ...)
		public function loadPage(startpage : int, endpage : int) : Boolean
		{
			if (m_load_status != STATUS_CHECKING && m_load_status != STATUS_COMPLETED)
				return false;

			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			var loadArray: Array;
			
			if (mmlType == 1) {
				loadArray = m_arrMmlPage;
				return true;
			}
			else {
				loadArray = m_arrLoader;
			
			
				var req : URLRequest;
				var params : URLVariables;
	
				var p1 : int = -1;
				var p2 : int;
				
				startpage = Math.max(0, startpage-1);
				endpage = Math.min(endpage-1, loadArray.length - 1);
				
				var bSend : Boolean = false;
				
				for (var i:int=startpage; i<=endpage; i++)
				{
					if (loadArray[i] != null)
					{
						if (p1 == -1)
							continue;
						
						p2 = i - 1;
					}
					else
					{
						if (p1 == -1)
						{
							p1 = i;
						}
	
						if (i < endpage)
							continue;
						
						p2 = i;
					}	
						
					req = new URLRequest(); 
					req.url = m_url; 
					params = new URLVariables();
					params["User-Agent"] = "RD Flash Viewer";
					params["opcode"] = "602";
					params["startpage"] = p1 + 1;
					params["endpage"] = p2 + 1;
					params["rdid"] = m_rdid;
					req.data = params; 
					req.method=URLRequestMethod.POST;
					 
					m_reportLoader.load(req);
					
					bSend = true;
				}
				
				return (bSend ? false : true);
			}
		}

		// event handler for http response header from ReportLoader
		private function httpResponseHandler(params : URLVariables, data : ByteArray) : void
		{
			// cancel status
			if (m_load_status == STATUS_CANCELED)
				return;
				
			var rescode : int;		// 100, 200, 300, 500
			var message : String;
			var total_page : int;
			var size : uint;
			
			m_httpResponseHandler.call(null, params.opcode);
			
			// 보고서 생성 요청
			if (params.opcode == "600")			
			{
				// rescode				
				size = data.readUnsignedInt();
				rescode = int(data.readUTFBytes(size));
				
				// Message
				size = data.readUnsignedInt();
				message = data.readUTFBytes(size);

				if (rescode == 100)
				{
					// RDID
					size = data.readUnsignedInt();
					m_rdid = data.readUTFBytes(size);

					// total_page				
					size = data.readUnsignedInt();
					total_page = int(data.readUTFBytes(size));
	
					// mrf length
					size = data.readUnsignedInt();
	
					if (total_page > 0 && size > 0)				
						m_reportLoader.parsePage(1, total_page, data);
					
					changeStatus(STATUS_CHECKING, false);

					// kth add 2012.04.12
					loadAccessibility();
					
					checkCompleted();
					Debug.trace("checkCompleted");
				}
				else
				{
					changeStatus(STATUS_INIT, false);
					
					m_documentLoadedErrorHandler.call(null, message);
				}
			}
			// 보고서 생성 완료 확인 요청
			else if (params.opcode == "601")	
			{
				// rescode				
				size = data.readUnsignedInt();
				rescode = int(data.readUTFBytes(size));
				
				// Message
				size = data.readUnsignedInt();
				message = data.readUTFBytes(size);

				if (rescode == 200)			// creating
				{
					// total_page				
					size = data.readUnsignedInt();
					total_page = int(data.readUTFBytes(size));
				}
				else if (rescode == 300)	// completed
				{
					// total_page				
					size = data.readUnsignedInt();
					total_page = int(data.readUTFBytes(size));
					
					m_reportLoader.clearCheckRequest();
					
					// mrf length
					size = data.readUnsignedInt();
	
					if (total_page > 0 && size > 0)				
						m_reportLoader.parsePage(1, 1, data);
					
					// kth add 2012.04.12
					loadAccessibility();
					
					changeStatus(STATUS_COMPLETED, m_save_report);
				}
				else
				{
					changeStatus(STATUS_CANCELED, false);
					
					m_documentLoadedErrorHandler.call(null, message);
				}	
			}
			// 페이지 이동 요청
			else if (params.opcode == "602")	
			{
				// rescode				
				size = data.readUnsignedInt();
				rescode = int(data.readUTFBytes(size));
				
				// Message
				size = data.readUnsignedInt();
				message = data.readUTFBytes(size);

				if (rescode == 100)		// ok
				{
					// total_page				
					size = data.readUnsignedInt();
					total_page = int(data.readUTFBytes(size));
	
					// mrf length
					size = data.readUnsignedInt();
	
					if (total_page > 0 && size > 0)				
						m_reportLoader.parsePage(params.startpage, params.endpage - params.startpage + 1, data);
				}
				else
				{
					m_documentLoadedErrorHandler.call(null, message);
				}
			}
			else if (params.opcode == "603")	// 보고서 Export 요청
			{
				// rescode				
				size = data.readUnsignedInt();
				rescode = int(data.readUTFBytes(size));
				
				// Message
				size = data.readUnsignedInt();
				message = data.readUTFBytes(size);

				changeStatus(m_load_status, false);
				
				if (rescode == 100)		// ok
				{
					// report length
					size = data.readUnsignedInt();
					
					m_reportByteArray = new ByteArray();
					data.readBytes(m_reportByteArray, 0, size);
					m_reportType = params["export_type"];
					
					var resourceManager:IResourceManager = ResourceManager.getInstance();
					
					// 
					//Debug.trace("checkCompleted");
			        Alert.show(resourceManager.getString("FlashViewer", "confirmSave"),
			        	resourceManager.getString("FlashViewer", "save"),
			        	Alert.YES | Alert.NO , null, SaveReportClickListner, null, Alert.YES);
				}
				else
				{
					m_documentLoadedErrorHandler.call(null, message);
				}
			}
			// 보고서 Export 중지 요청
			else if (params.opcode == "604")	
			{
				// rescode				
				size = data.readUnsignedInt();
				rescode = int(data.readUTFBytes(size));
				
				// Message
				size = data.readUnsignedInt();
				message = data.readUTFBytes(size);

				changeStatus(m_load_status, false);
				
				if (rescode == 100)		// ok
				{
					// report length
					size = data.readUnsignedInt();
					
					m_reportByteArray = new ByteArray();
					data.readBytes(m_reportByteArray, 0, size);
					m_reportType = params["export_type"];
					
					var resourceManager1:IResourceManager = ResourceManager.getInstance();
					
					// 
			        Alert.show(resourceManager1.getString("FlashViewer", "confirmSave"),
			        	resourceManager1.getString("FlashViewer", "save"),
			        	Alert.YES | Alert.NO , null, SaveReportClickListner, null, Alert.YES);
				}
				else
				{
					m_documentLoadedErrorHandler.call(null, message);
				}
			}
			// kth add 2012.04.12
			// request accessibility text
			else if (params.opcode == "606")	
			{
				var tmpStr:String;
				
				// rescode				
				size = data.readUnsignedInt();
				rescode = int(data.readUTFBytes(size));
				
				// Message
				size = data.readUnsignedInt();
				message = data.readUTFBytes(size);
				
				if (rescode == 100)		// ok
				{
					// report length
					size = data.readUnsignedInt();
					tmpStr = data.readUTFBytes(size);
					arryAccessibilityTxt[m_curPage] = tmpStr;
					
					// kth add 2012.04.12
					updateAccessibilityPropHandler.call(null);
				}
				else
				{
					arryAccessibilityTxt[m_curPage] = "";
					// 606에서는 error message를 보여주지 않는다. 
					//m_documentLoadedErrorHandler.call(null, message);
				}
			}
			else
			{
				return;
			}
			
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			var loadArray: Array;
			
			if (mmlType == 1)
				loadArray = m_arrMmlPage;
			else
				loadArray = m_arrLoader;
			
			var bPageCountChanged : Boolean = false;
			
			for(var i : int=loadArray.length; i<total_page; i++)
			{
				loadArray.push(null);
				
				bPageCountChanged = true;
			}
			
			if (bPageCountChanged)
				m_pageCountChangedHandler.call(null, loadArray.length);
		}
		
		// kth #1461 [한국대학교육협의회] Flash Reporting 동작 방식 변경
		private function httpProgressHandler(data: ByteArray, page: int) : void
		{
			// cancel status
			if (m_load_status == STATUS_CANCELED)
				return;
			
			var size: uint;
			
			if (data != null)
			{
				var swfSize: uint;
				var txtSize: uint;
				
				var swfBytes: ByteArray = new ByteArray();
				var tmpStr: String;
				var tmpInt: int;
				
				// swf
				swfSize = data.readUnsignedInt();
				data.readBytes(swfBytes, 0, swfSize);
				m_reportLoader.parseSwfPage(page+1, swfBytes);
				
				// txt
				txtSize = data.readUnsignedInt();
				tmpStr = data.readUTFBytes(txtSize);
				arryAccessibilityTxt[page] = tmpStr;
				// kth add 2012.04.12
				updateAccessibilityPropHandler.call(null);
				
				// next page
				tmpInt = data.readUnsignedInt();
				if (tmpInt == 0) // last pge
					changeStatus(STATUS_COMPLETED, false);
				else
					changeStatus(STATUS_CHECKING, false);
				
				var bPageCountChanged : Boolean = false;
				
				for(var i : int=m_arrLoader.length; i<page+1; i++) // total page update
				{
					m_arrLoader.push(null);
					
					bPageCountChanged = true;
				}
				
				if (bPageCountChanged)
					m_pageCountChangedHandler.call(null, m_arrLoader.length);
			}
			else
			{
				// if data is null, page is status
				if (page >= STATUS_INIT && page <= STATUS_CANCELED)
					changeStatus(page, false);
				else
					changeStatus(STATUS_INIT, false);
			}
		}
		
		// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
		private function httpMmlHandler(data: String, page: int) : void
		{
			// cancel status
			if (m_load_status == STATUS_CANCELED)
				return;
			
			var size: uint;
			
			if (data != null)
			{
				if (page == -1) {
					m_mmlHead = data;
					var headXML:XML = XML(data);
					Debug.trace("headData  : " + data);
					commonModel.mml.head.dataProvider = headXML;
					commonModel.mml.head.parser();
					Debug.trace("---");
					Debug.trace(ObjectUtil(commonModel.mml));
					Debug.trace("---");
					return;
				}

				m_arrMmlPage[page] = data;
				commonModel.pageArray = m_arrMmlPage;
				//Debug.trace("page : " + page);
				m_pageChangedHandler.call(null, page+1);	
				
				var bPageCountChanged : Boolean = false;
				for(var i : int=m_arrMmlPage.length; i<page+1; i++) // total page update
				{
					m_arrMmlPage.push(null);
					bPageCountChanged = true;
				}
				
				if (bPageCountChanged)
					m_pageCountChangedHandler.call(null, m_arrMmlPage.length);
			}
			else
			{
				// if data is null, page is status
				if (page >= STATUS_INIT && page <= STATUS_CANCELED)
					changeStatus(page, false);
				else
					changeStatus(STATUS_INIT, false);
			}
		}
		
		//
		private function SaveReportClickListner(btnClicked:Object) : void
		{
			switch (btnClicked.detail) 
			{
				case Alert.YES:
				{
					var _fR:FileReference = new FileReference();
					_fR.save(m_reportByteArray, "report." + m_reportType);
					break;
				}
				case Alert.NO:
					break;
			}
		}
		
		//
		private function changeStatus(status : int, saving : Boolean) : void
		{
			if (status == m_load_status && m_save_report == saving)
				return;
				
			m_load_status = status;
			m_save_report = saving;
			
			m_statusChangedHandler.call(null);
		}

		// kth add 2012.04.18
		public function isReportDone(): Boolean
		{
			var retVal: Boolean;
			
			if (m_load_status == STATUS_COMPLETED || m_load_status == STATUS_CANCELED)
				retVal = true;
			else
				retVal = false;
			return retVal;
		}
		
		// event handler for SWF file loadded
		private function pageLoadHandler(event:Event) : void
		{
			// cancel status
			if (m_load_status == STATUS_CANCELED)	
				return;
				
			var page : Object = event.target.content;
			var loader : Loader = null;
			var pageno : int;
			
			if(page != null && page is MovieClip)			// SWF 9
			{
				loader = page.parent as Loader;
			}	
			else if(page != null && page is AVM1Movie)		// SWF 7, 8
			{
				loader = page.parent as Loader;
			}
			
			if (loader != null)
			{
				pageno = parseInt(loader.name.substr(11));	
				
				m_arrLoader[pageno-1] = loader;
				
				m_pageChangedHandler.call(null, pageno);	
			}				
		}

		//
		public function get currentPage() : int
		{
			return m_curPage + 1;	
		}
		
		// kth add 2012.04.12	
		public function loadAccessibility() : void
		{
			if (!accessibility_active)
				return;
			
			var req : URLRequest = new URLRequest(); 
			req.url = m_url; 
			
			var params : URLVariables = new URLVariables();
			params["User-Agent"] = "RD Flash Viewer";
			params["opcode"] = "606";
			params["rdid"] = m_rdid;
			params["page"] = m_curPage+1;
			
			req.data = params; 
			req.method=URLRequestMethod.POST;
			
			m_reportLoader.load(req);
		}
		
		// kth add 2012.04.12
		public function setAccessibilityHandler(paramHandler : Function): void
		{
			updateAccessibilityPropHandler = paramHandler;
		}
		
		// kth add 2012.04.12
		public function getAccessibilityText(page:int): String
		{
			if (!accessibility_active)
			{
				return "";
			}
			
			var tmpStr: String;
			if (page > arryAccessibilityTxt.length)
			{
				loadAccessibility();
				if (page > arryAccessibilityTxt.length || arryAccessibilityTxt[page-1] == null)
				{
					return "";
				}
				else
				{
					tmpStr = arryAccessibilityTxt[page-1].toString();
					return tmpStr;
				}
			}
			else
			{
				if (arryAccessibilityTxt[page-1] == null)
				{
					loadAccessibility();
					if (arryAccessibilityTxt[page-1] == null)
					{
						return "";
					}
				}
				
				tmpStr = arryAccessibilityTxt[page-1].toString();
				return tmpStr;
			}
		}
		
		// kth add 2012.04.12
		public function setAccessibilityActive(active:Boolean): void
		{
			accessibility_active = active;
		}

		// kth add 2012.04.12
		public function setAccessibilityOpt(opt: int): void
		{
			accessibility_opt = opt;
		}
		
		// kth #1461 [한국대학교육협의회] Flash Reporting 동작 방식 변경
		public function setSwfStreamOpt(opt: int): void
		{
			swfStream_opt = opt;
			m_reportLoader.setSwfStreamOpt(opt);
		}
		
		// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
		public function setMmlType(type: int): void
		{
			mmlType = type;
			m_reportLoader.setMmlType(type);
		}

		public function getMmlType(): int
		{
			return mmlType;
		}
		
		public function changePage(page: int): void 
		{
			var curPage:String;
			curPage = m_arrMmlPage[page-1];
			m_documentLoadedErrorHandler.call(null, curPage); // error 가 아니나 message를 표시하기 위해서 call
		}
		
		public function resetPage(): void
		{
			m_arrLoader = new Array();
			m_arrMmlPage = new Array();
		}
		
		// kth add 2012.08.21
		public function setProgressTimeout(timeout: int): void
		{
			m_reportLoader.setProgressTimeout(timeout);
		}
		
		// set current page (1 - )
		public function gotoPage(start : int, end : int):void
		{
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			var loadArray: Array;
			
			if (mmlType == 1)
				loadArray = m_arrMmlPage;
			else
				loadArray = m_arrLoader;
			
			if (start <= 0 || start > loadArray.length)
				return;
				
			m_curPage = start - 1;
			
			if (mmlType == 1) {
				var curPage: String;
				curPage = loadArray[m_curPage];
				m_documentLoadedErrorHandler.call(null, curPage); // error 가 아니나 message를 표시하기 위해서 call
				return;
			}
			
			if (loadArray[m_curPage] == null)
				loadPage(m_curPage+1, m_curPage+1+(end - start));
		}

		//
		public function get status() : int
		{
			return m_load_status;
		}
		
		//
		public function get saving() : Boolean
		{
			return m_save_report;
		}
		
		//
		public function get pageCount() : int
		{
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			var loadArray: Array;
			
			if (mmlType == 1)
				loadArray = m_arrMmlPage;
			else
				loadArray = m_arrLoader;
			
			return loadArray.length;
		}

		//
		public function get width():Number
		{
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			var loadArray: Array;
			
			if (mmlType == 1)
				loadArray = m_arrMmlPage;
			else
				loadArray = m_arrLoader;
			
			if (m_curPage < 0 || m_curPage >= loadArray.length)
				return 0;
			
			if (loadArray[m_curPage] == null)
				return 0;
				
			if (mmlType == 1)
				return 0;
			else
				return (loadArray[m_curPage] as Loader).width;
		}

		//
		public function get height():Number
		{
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			var loadArray: Array;
			
			if (mmlType == 1)
				loadArray = m_arrMmlPage;
			else
				loadArray = m_arrLoader;
			
			if (m_curPage < 0 || m_curPage >= loadArray.length)
				return 0;
			
			if (loadArray[m_curPage] == null)
				return 0;
				
			if (mmlType == 1)
				return 0;
			else
				return (loadArray[m_curPage] as Loader).height;
		}

		//
		public function get pageLoader() : Loader
		{
			// kth 2343 [Flash Viewer] mml 포맷을 읽어서 표현하도록 지원. head, page별 xml 저장함
			if (mmlType == 1)
				null;
			
			if (m_curPage < 0 || m_curPage >= m_arrLoader.length)
				return null;
			
			if (mmlType == 1)
				return null;
			
			return m_arrLoader[m_curPage];
		}

		public function dispatchEvent(event:Event):Boolean {
			return dispatcher.dispatchEvent(event);
		}
		
		//
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
			dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		//
		public function hasEventListener(type:String):Boolean {
			return dispatcher.hasEventListener(type);
		}
		
		//
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
			dispatcher.removeEventListener(type, listener, useCapture);
		}
		
		//
		public function willTrigger(type:String):Boolean {
			return dispatcher.willTrigger(type);
		}				
	}
}